/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package GA;

import tetris.Game;
import tetris.NNAi;
import tetris.TetrisAI;

/**
 *
 * @author S094182
 */
public class NNTetrisEnvironment implements IEnvironment {
    
    protected Game game;
    protected IEvolvingAI ai;    
    
    public NNTetrisEnvironment(Game game) {
        this.game = game;
        TetrisAI tai = new NNAi();
        game.setAi(tai);
        ai = (IEvolvingAI)tai;
    }

    @Override
    public int getGeneLength() {
        int n = ai.getNGenes();
        return n*8;
        
    }

    @Override
    public int getFitnessType() {
        return BinaryGenome.MAXIMIZE;
    }

    @Override
    public double evaluateGenome(BinaryGenome gene) {
        if (gene.isFitnessSet())
            return gene.getFitness();       
        ai.setHeuristic(gene.getGenome());
        double score = game.run();
        gene.setFitness(score);
        ai.feedback(new double[] { 1 - (score * (1d/1000000) )});
        double fitness = gene.getFitness();
        return fitness;
    }
}
